﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using Public;

namespace DAL
{
    public class TaiKhoan_DAL
    {
        Connection conn = new Connection();
        TaiKhoan_PB tk_pb = new TaiKhoan_PB();

        #region "Đăng nhập"
        public DataTable Login(string taikhoan, string matkhau)
        {
            string sql = string.Format("select * from TAIKHOAN where TENDANGNHAP='{0}' and MATKHAU=N'{1}'", taikhoan, matkhau);
            return conn.LoadData(sql);
        }
        #endregion

        #region Lấy mật khẩu
        public DataTable LayMatKhau(string taikhoan)
        {
            string sql = "select MATKHAU from TAIKHOAN where TENDANGNHAP='" + taikhoan + "'";
            return conn.LoadData(sql);
        }
        #endregion

        #region "Lấy DS đơn vị"
        public DataTable LayDSDonVi()
        {
            return conn.LoadData("select * from DONVI");
        }
        #endregion

        #region "Kiểm tra tồn tại"

        #endregion


        #region Thêm tài khoản
        public int ThemTaiKhoan(TaiKhoan_PB tk_pb)
        {
            int nparameter = 6;
            string[] name = new string[nparameter];
            object[] value = new object[nparameter];
            name[0] = "@taikhoan"; value[0] = TaiKhoan_PB.TenTaiKhoan;
            name[1] = "@matkhau"; value[1] = TaiKhoan_PB.MatKhau;
            name[2] = "@hoten"; value[2] = TaiKhoan_PB.HoTen;
            name[3] = "@donvi"; value[3] = TaiKhoan_PB.DonVi;
            name[4] = "@email"; value[4] = TaiKhoan_PB.Email;
            name[5] = "@hinhanh"; value[5] = tk_pb.HinhAnh;
            string sql = "insert into TAIKHOAN values (@taikhoan,@matkhau,@hoten,@donvi,@email,@hinhanh)";
            return conn.Update(sql, name, value, nparameter);
        }
        #endregion

        #region "Sửa tài khoản"
        public int SuaTaiKhoan(TaiKhoan_PB tk_pb)
        {
            int nparameter = 6;
            string[] name = new string[nparameter];
            object[] value = new object[nparameter];
            name[0] = "@taikhoan"; value[0] = TaiKhoan_PB.TenTaiKhoan;
            name[1] = "@matkhau"; value[1] = TaiKhoan_PB.MatKhau;
            name[2] = "@hoten"; value[2] = TaiKhoan_PB.HoTen;
            name[3] = "@donvi"; value[3] = TaiKhoan_PB.DonVi;
            name[4] = "@email"; value[4] = TaiKhoan_PB.Email;
            name[5] = "@hinhanh"; value[5] = tk_pb.HinhAnh;
            string sql = "update TAIKHOAN set MATKHAU=@matkhau,HOTEN=@hoten,DONVI=@donvi,EMAIL=@email,HINHANH=@hinhanh where TENDANGNHAP=@taikhoan";
            return conn.Update(sql, name, value, nparameter);
        }
        #endregion

        #region "Xóa tài khoản"
        public int XoaTaiKhoan(TaiKhoan_PB tk_pb)
        {
            int nparameter = 1;
            string[] name = new string[nparameter];
            object[] value = new object[nparameter];
            name[0] = "@taikhoan"; value[0] = TaiKhoan_PB.TenTaiKhoan;
            string sql = "delete from TAIKHOAN where TENDANGNHAP=@taikhoan";
            return conn.Update(sql, name, value, nparameter);
        }
        #endregion

        #region "Lấy ds tài khoản"
        public DataTable LayDSTaiKhoan()
        {
            return conn.LoadData("select *,dv.TENDONVI from TAIKHOAN tk,DONVI dv where tk.DONVI=dv.MADONVI");
        }
        #endregion

        #region "Kiểmt tra mật khẩu có tồn tại ko?"
        public bool CheckExists(string taikhoan, string matkhau)
        {
            if ((conn.CheckExistValues("TAIKHOAN", "MATKHAU", taikhoan, matkhau)))
                return true;
            return false;
        }
        #endregion

        #region "Thay đổi MK"
        public int ThayDoiMK(TaiKhoan_PB tk_pb)
        {
            int nparameter = 2;
            string[] name = new string[nparameter];
            object[] value = new object[nparameter];
            name[0] = "@tentk"; value[0] = TaiKhoan_PB.TenDangNhap;
            name[1] = "@matkhaumoi"; value[1] = TaiKhoan_PB.MatKhauMoi;
            string sql = "update TAIKHOAN set MATKHAU=@matkhaumoi where TENDANGNHAP=@tentk";
            return conn.Update(sql, name, value, nparameter);
        }
        #endregion
    }
}
